###############################################################################
# CA cleaning
#
# 11 August 2022
# sbaltz
###############################################################################

import pandas as pd

RAW_FILE_NAME = "../transcribed/mariposa.csv"
SAVE_FILE_NAME = "../ready/mariposa_cleaned.csv"
COUNTY_NAME = "MARIPOSA"

ct = pd.read_csv(RAW_FILE_NAME)

cleaned = pd.DataFrame()

for i in range(len(ct)):
    if "Contest" in str(ct["candidate"][i]):
        office = str(ct["office"][i])
    if str(ct["original"][i]) != "nan" and str(ct["audited"][i]) != "nan":
        try:
            state = "CALIFORNIA"
            county = COUNTY_NAME
            candidate = ct["candidate"][i]
            original = int(ct["original"][i])
            audited = int(ct["audited"][i])
            difference = audited - original
            method = "MANUAL"
            if candidate != "Total":
                cleaned = pd.concat([cleaned,
                            pd.DataFrame([[state,county,office,\
                            candidate,original,audited,difference,method]])],\
                            ignore_index = True)
        except Exception as inst:
            print("ROW NOT SAVED: ", inst)

cleaned.columns = ["state","county","office","candidate","original","audited",\
                   "difference","type"]

cleaned.to_csv(SAVE_FILE_NAME, index=False)
